package com.mine.modules.vm.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mine.modules.vm.domain.query.FieldStatsQuery;
import com.mine.modules.vm.entity.VmAssets;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author xujf
 * @since 2024-09-21
 */
public interface VmAssetsMapper extends BaseMapper<VmAssets> {

/*    @Select("SELECT b.*,a.scene_layer_id as elemment_scene_layer_id,a.id as sid " +
            "FROM vm_layer_element a " +
            "LEFT JOIN vm_assets b ON a.element_id=b.id WHERE b.layer_id IN (#{layerIdList}) ")*/

    @Select({
            "<script>",
            "SELECT b.*, a.scene_layer_id AS element_scene_layer_id, a.id AS sid ",
            "FROM vm_layer_element a ",
            "LEFT JOIN vm_assets b ON a.element_id = b.id ",
            "WHERE a.element_type='asset_device' AND a.layer_id IN ",
            "   <foreach collection='layerIdList' item='id' open='(' separator=',' close=')'>",
            "       #{id}",
            "   </foreach>",
            "   <if test='keyword != null and keyword != \"\"'>",
            "       AND (b.asset_name LIKE CONCAT('%', #{keyword}, '%') or b.asset_number LIKE CONCAT('%', #{keyword}, '%') ) ",
            "   </if>",
            "   <if test='assetClassify != null and assetClassify != \"\"'>",
            "       AND b.asset_classify LIKE CONCAT('%', #{assetClassify}, '%')",
            "   </if>",
            "   <if test='location != null and location != \"\"'>",
            "       AND b.install_position LIKE CONCAT('%', #{location}, '%')",
            "   </if>",
            "   <if test='deviceClassify != null and deviceClassify != \"\"'>",
            "       AND b.device_classify LIKE CONCAT('%', #{deviceClassify}, '%')",
            "   </if>",
            "   <if test='purpose != null and purpose != \"\"'>",
            "       AND b.purpose LIKE CONCAT('%', #{purpose}, '%')",
            "   </if>",
            "</script>"
    })
    IPage<VmAssets> selectPageWithElement(
            Page<?> page,
            @Param("layerIdList") List<String> layerIdList,
            @Param("keyword") String keyword,
            @Param("assetClassify") String assetClassify,
            @Param("location") String location,
            @Param("deviceClassify") String deviceClassify,
            @Param("purpose") String purpose
    );

    List<Map<String, Object>> selectDynamicFieldStats(@Param("query") FieldStatsQuery query);

}
